<?php
/**
 * Created by PhpStorm.
 * User: ytlmi
 * Date: 2018/1/1
 * Time: 23:39
 */

namespace org\util\wordtool;


class TableRow
{
    public $page;
    public $tbl;
    public $tr;
    //默认行高
    protected $lineHeight = 400;

    public function __construct(\DOMNode $tbl,$style)
    {
        $this->page=$tbl->ownerDocument;
        $this->tbl=$tbl;
        $this->tr=$this->page->createElement('w:tr');
        $this->addTrPr($style);
        $this->tbl->appendChild($this->tr);
    }

    /**
     * @param int $height
     * @return TableRow
     * @throws \Exception
     * 设置行高
     */
    public function setLineHeight($height)
    {
        if (!is_int($height)) throw new \Exception('Wrong Line Height:' . $height);
        $this->lineHeight = $height;
        return $this;
    }

    /**
     * @param array $style
     * 设置行样式
     */
    protected function addTrPr($style)
    {
        //检测参数
        if (is_array($style && isset($style['height']))){
            $height=$style['height'];
        }elseif(is_int($style+0) && $style!=null ){
            $height=$style;
        }else{
            $height=$this->lineHeight;
        }
        //生成行样式节点
        $trPr = $this->page->createElement('w:trPr');
        $trHeight = $this->page->createElement('w:trHeight');
        $trHeight->setAttribute('w:val', $height);
        $trPr->appendChild($trHeight);
        $this->tr->appendChild($trPr);
    }

    /**
     * @param mixed $font
     * @param array $style
     * @return Content
     * @throws \Exception
     * 添加单元格
     */
    public function addCell($font=null,$style=null)
    {
        //生成一个单元格
        $tc = $this->page->createElement('w:tc');
        //添加单元格样式
        $objStyle=new Style();
        try{
            $tcPr=$objStyle->createTCPR($style);
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }

        $tc->appendChild($this->page->importNode($tcPr,true));
        $tc= $this->tr->appendChild($tc);
        return new Content($tc,$font);
    }

}